package com.yuxiang.flowable.service.impl;

import com.yuxiang.flowable.service.FlowableService;
import org.flowable.engine.DynamicBpmnService;
import org.flowable.engine.FormService;
import org.flowable.engine.HistoryService;
import org.flowable.engine.IdentityService;
import org.flowable.engine.ManagementService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.engine.task.Comment;
import org.flowable.task.api.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author : yx-0176
 * @description
 * @date : 2022/3/9
 */
@Service
public class FlowableServiceImpl implements FlowableService {

    Logger log = LoggerFactory.getLogger(FlowableServiceImpl.class);


    @Autowired
    TaskService taskService;

    @Autowired
    HistoryService historyService;

    @Autowired
    RepositoryService repositoryService;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    ManagementService managementService;

    @Autowired
    IdentityService identityService;

    @Autowired
    DynamicBpmnService dynamicBpmnService;

    @Autowired(required = false)
    FormService formService;


    @Override
    public String startProcessInstance(String key, Object variables) {
        ProcessInstance assessmentProcess = runtimeService.startProcessInstanceByKey(key, objectToMap(variables));
        return assessmentProcess.getId();
    }

    @Override
    public void endProcessInstance(String processId, String reason ) {
        runtimeService.deleteProcessInstance(processId,reason);
    }

    @Override
    public void handleProcessTask(String taskId, Object variables) {
        log.info("handle task ${} info ${}", taskId, variables.toString());
        Map<String, Object> variable = taskService.getVariables(taskId);
        variable.putAll(objectToMap(variables));
        taskService.complete(taskId, variable);
    }

    @Override
    public List<Task> getTaskList(String taskCandidateGroup) {
        if (null==taskCandidateGroup){
            return taskService.createTaskQuery().list();
        }
        return taskService.createTaskQuery().taskCandidateGroup(taskCandidateGroup).list();
    }

    @Override
    public Object getTaskInfo(String taskId) {
        Map<String, Object> processVariables = taskService.getVariables(taskId);
        System.out.println(processVariables);
        return processVariables;
    }

    @Override
    public List<HistoricActivityInstance> getHistory(String processId) {
        List<HistoricActivityInstance> hvis = historyService
                .createHistoricActivityInstanceQuery()
                .processInstanceId(processId)
                .activityType("userTask")
                .orderByHistoricActivityInstanceEndTime()
                .asc()
                .list();
        for (HistoricActivityInstance hai : hvis) {
            //根据任务id去历史意见表查询数据
            List<Comment> taskComments = taskService.getTaskComments(hai.getTaskId());
            System.out.println(taskComments);

        }

        return hvis;
    }
}
